package spotGUI.personalProfileGUI;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

import managers.DBManager;
import managers.socialNetworkManagers.SocialNetworkFeedbackManager;
import managers.socialNetworkManagers.SocialNetworkManager;
import personalProfile.PhotoManager;
import personalProfile.PhotoManager.Photo;
import personalProfile.PhotoManager.PhotoAlbum;
import spotGUI.utils.EmptyContentAreaButton;
import utils.StackTracePrinter;
import java.util.ResourceBundle;

public class BigPhotoView extends JFrame {

	private JButton btnPrevious;
	private JButton btnNext;
	private JTextPane textAreaDescription;
	private JLabel lblAlbumtitle;

	private PhotoManager manager;
	private ArrayList<Photo> photos;
	private int currentPosition;

	private ImagePanel imagePanel;
	private GridBagLayout gridBagLayout;
	
	public BigPhotoView (final PhotoAlbum album, final Photo firstPhoto, PhotoManager manager) {

		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(10, 10 ,10 ,10));
		setContentPane(contentPane);

		setSize(new Dimension(800, 600));
		setMinimumSize(new Dimension(400, 300));
		setLocationRelativeTo(null);
		
		this.manager = manager;
		this.photos = album.photos;
		currentPosition = album.photos.indexOf(firstPhoto);

		setTitle(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("BigPhotoView.title")+album.name);
		setIconImage(Toolkit.getDefaultToolkit().getImage(BigPhotoView.class.getResource("/images/Icons/64x64/users_comments.png")));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{50, 349, 52, 0};
		gridBagLayout.rowHeights = new int[]{52, 61, 52, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);

		lblAlbumtitle = new JLabel("albumTitle");
		lblAlbumtitle.setFont(new Font("Agency FB", Font.BOLD, 39));
		lblAlbumtitle.setHorizontalAlignment(SwingConstants.CENTER);
		GridBagConstraints gbc_lblAlbumtitle = new GridBagConstraints();
		gbc_lblAlbumtitle.anchor = GridBagConstraints.NORTH;
		gbc_lblAlbumtitle.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblAlbumtitle.insets = new Insets(0, 0, 5, 0);
		gbc_lblAlbumtitle.gridwidth = 3;
		gbc_lblAlbumtitle.gridx = 0;
		gbc_lblAlbumtitle.gridy = 0;
		getContentPane().add(lblAlbumtitle, gbc_lblAlbumtitle);



		btnPrevious = new EmptyContentAreaButton(BigPhotoView.class.getResource("/images/Icons/48x48px/back.png"), null);
		btnPrevious.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnNext.setEnabled(true);
				currentPosition--;
				if (currentPosition == 0) {
					btnPrevious.setEnabled(false);
				}
				showPhoto(photos.get(currentPosition), album);
			}
		});
		btnPrevious.setToolTipText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("BigPhotoView.btnPrevious.toolTipText")); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_btnPrevious = new GridBagConstraints();
		gbc_btnPrevious.anchor = GridBagConstraints.WEST;
		gbc_btnPrevious.fill = GridBagConstraints.VERTICAL;
		gbc_btnPrevious.insets = new Insets(0, 0, 5, 5);
		gbc_btnPrevious.gridx = 0;
		gbc_btnPrevious.gridy = 1;
		getContentPane().add(btnPrevious, gbc_btnPrevious);


		imagePanel = new ImagePanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 1;
		gbc_panel.gridy = 1;
		contentPane.add(imagePanel, gbc_panel);
		
		btnNext = new EmptyContentAreaButton(BigPhotoView.class.getResource("/images/Icons/48x48px/next.png"), null);
		btnNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnPrevious.setEnabled(true);
				currentPosition++;
				if (currentPosition == photos.size()-1) {
					btnNext.setEnabled(false);
				}
				showPhoto(photos.get(currentPosition), album);
			}
		});

		btnNext.setToolTipText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("BigPhotoView.btnNext.toolTipText")); //$NON-NLS-1$ //$NON-NLS-2$
		GridBagConstraints gbc_btnNext = new GridBagConstraints();
		gbc_btnNext.anchor = GridBagConstraints.WEST;
		gbc_btnNext.fill = GridBagConstraints.VERTICAL;
		gbc_btnNext.insets = new Insets(0, 0, 5, 0);
		gbc_btnNext.gridx = 2;
		gbc_btnNext.gridy = 1;
		getContentPane().add(btnNext, gbc_btnNext);

		textAreaDescription = new JTextPane();
		textAreaDescription.setOpaque(false);
		textAreaDescription.setEditable(false);
		SimpleAttributeSet attribs = new SimpleAttributeSet();  
		StyleConstants.setAlignment(attribs , StyleConstants.ALIGN_LEFT);  
		StyleConstants.setFontFamily(attribs, "Dialog");
		StyleConstants.setFontSize(attribs, 12);
		textAreaDescription.setParagraphAttributes(attribs,true);  
		GridBagConstraints gbc_testAreaDescription = new GridBagConstraints();
		gbc_testAreaDescription.fill = GridBagConstraints.BOTH;
		gbc_testAreaDescription.gridwidth = 3;
		gbc_testAreaDescription.gridx = 0;
		gbc_testAreaDescription.gridy = 2;
		getContentPane().add(textAreaDescription, gbc_testAreaDescription);

		if (currentPosition == 0) {
			btnPrevious.setEnabled(false);
		}
		
		if (currentPosition == photos.size()-1) {
			btnNext.setEnabled(false);
		}
		
		addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				showPhoto(photos.get(currentPosition), album);
			}
		});

	}




	private void showPhoto (Photo photo, PhotoAlbum album) {
		lblAlbumtitle.setText(album.name);
		if (!photo.description.equals("")) {
			textAreaDescription.setText(photo.description);
		} else {
			textAreaDescription.setText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("BigPhotoView.description"));
		}
		try {
			
			ImageIcon bigPhoto = manager.retrieveBigPhoto(photo, DBManager.getConnection());
			
			double ratio = (double)bigPhoto.getIconWidth()/(double)bigPhoto.getIconHeight();
			int width = (int) Math.min(Math.min(imagePanel.getWidth(), imagePanel.getHeight()*ratio), bigPhoto.getIconWidth());
			int height = (int) (width/ratio);
			
			imagePanel.drawScaledImage(bigPhoto, width, height);
			
		} catch (Exception e) {
			lblAlbumtitle.setText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("BigPhotoView.error2"));
			textAreaDescription.setText(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("BigPhotoView.error"));
			SocialNetworkManager.getSharedInstance().getFeedbackManager().updateStatus(ResourceBundle.getBundle("spotGUI.personalProfileGUI.messages").getString("BigPhotoView.error"), SocialNetworkFeedbackManager.WARNING);
			StackTracePrinter.print(e);
		}
	}



	private class ImagePanel extends JPanel {

		private Graphics g;
		private ImageIcon img;
		private int width;
		private int height;


		@Override
		protected void paintComponent(Graphics g) {
			this.g = g;
			super.paintComponent(g);
			doDrawing();
		}

		public void drawScaledImage (ImageIcon img, int width, int height) {
			this.img = img;
			this.width = width;
			this.height = height;
			repaint();
		}

		private void doDrawing () {
			if (width > 0 && height > 0) {
				g.drawImage(img.getImage(), (imagePanel.getWidth()-width)/2, (imagePanel.getHeight()-height)/2, width, height, null);
				g.drawRect((imagePanel.getWidth()-width)/2, (imagePanel.getHeight()-height)/2, width-1, height-1);
				g.setColor(UIManager.getColor("Button.background"));
				g.drawRoundRect((imagePanel.getWidth()-width)/2+2, (imagePanel.getHeight()-height)/2+2, width-5, height-5, 5, 5);
			} else {
				throw new IllegalArgumentException("Invalid dimension for image scaling:"+width+" x "+height);
			}
		}
	}

}
